package cc.comac.mom.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.dao.entity.QualityQarVerifyPlanDao;
import cc.comac.mom.entity.QualityQarVerifyPlanEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityQarVerifyPlanDataHandler extends ServiceImpl<QualityQarVerifyPlanDao, QualityQarVerifyPlanEntity>{
	@Autowired
	private QualityQarVerifyPlanDao qualityQarVerifyPlanDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	
	public ResultBean insertBatch(List<QualityQarVerifyPlanEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		for(QualityQarVerifyPlanEntity item :list) {
			baseEntityDataUtil.setBaseEntityData(item);
		}
		boolean saveBatch = this.saveBatch(list);
		if(!saveBatch) {
			resultBean.setErrorMessge("保存验证信息失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultBean deleteBySid(QualityQarVerifyPlanEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;	
		}
    	QueryWrapper<QualityQarVerifyPlanEntity> queryWrapper = new QueryWrapper<QualityQarVerifyPlanEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("sid", obj.getSid());
		List<QualityQarVerifyPlanEntity> selectList = qualityQarVerifyPlanDao.selectList(queryWrapper);
		if(selectList == null || selectList.size()<1) {
			resultBean.setErrorMessge("查询不到该sid:"+obj.getSid()+" 对应的检验信息");
			return resultBean;	
		}
		for(QualityQarVerifyPlanEntity item :selectList) {
			item.setInvokerName(obj.getInvokerName());
			item.setInvokeDate(obj.getInvokeDate());
			baseEntityDataUtil.deleteBaseEntityData(item);		
		}
		boolean updateBatchById = this.updateBatchById(selectList);
		if(!updateBatchById) {
			resultBean.setErrorMessge("删除失败");
			return resultBean;	
		}
		return resultBean;
	}
}
